﻿using ConfExpert.Forms.Base;
using ConfExpert.Forms.DockingControls;
using ConfExpert.Forms.TaskManager;
using ConfExpert.Utility;
using ConfExpert.Utility.Ribbon;
using DevTeam;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms
{
    public partial class MainForm : DevMainForm
    {
        public MainForm()
        {
            InitializeComponent();

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //if (Session.AmI(4, 17, 94, 95, 120))

            if (AppSession.Settings.SkinName != string.Empty)
                DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(AppSession.Settings.SkinName);
            else
                DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle("Office 2013");

            Session.SetAdmins(4, 17, 94, 95, 284, 285, 192, 527);
            Session.SetDevelopers(17, 94, 95, 284, 285, 527);

            AppSession.OncekiKonf = new AppSession.clsOncekiKonf();
            AppSession.OncekiKonf.KonfTarihi = DateTime.Today;

            AppSession.OncekiYukleme = new AppSession.clsOncekiYukleme();
            AppSession.OncekiYukleme.YuklemeTarihi = DateTime.Today;

            ConfExpertRibbon.Init();
            Mail.Init();
            AppSession.EasyNav = new EasyNav();

            GridViewExtensions.Binding += GridViewExtensions_Binding;
            try
            {
                new AppSession();
            }
            catch (ApplicationException ex)
            {
                LogManager.LogToDB(ex);
                Application.Restart();
                return;
            }
            FileManager.CreateTempFolder("CEDocs", true);


            if (!Session.isProblematicUser)
            {
                JobManager.Job jobOP = JobManager.Add("Onayınızı bekleyen imalat fazlası girişleri var!", 480, false);
                jobOP.Alert += jobOP_Alert;
                jobOP.PopupClicked += jobOP_PopupClicked;

                JobManager.Job jobPublish = JobManager.Add("Yeni bir sürüm var!", 600, false);
                jobPublish.Alert += jobPublish_Alert;

                JobManager.Job jobOnay = JobManager.Add("Onayınızı bekleyen istekler var!", 600, true);
                jobOnay.Alert += jobOnay_Alert;

                JobManager.Job jobTasks = JobManager.Add("Görevler", 1200, true);
                jobTasks.Alert += jobTasks_Alert;

                JobManager.Job jobYeniSiparis = JobManager.Add("Onayınızı bekleyen yeni siparişler var!", 540, true);
                jobYeniSiparis.Alert += jobYeniSiparis_Alert;
                jobOnay.PopupClicked += jobYeniSiparis_PopupClicked;

                JobManager.CheckAlerts();
            }
            Reports reports = new Reports(nbReports, txtReportFilter, btnReportClearFilter);
            CustomerBasedOperations cbo = new CustomerBasedOperations(nbCustomer, gleCustomer);
            SeasonBasedOperations sbo = new SeasonBasedOperations(nbSeason, gleSeason);

            this.Persisting += MainForm_Persisting;
            this.UserChanged += MainForm_UserChanged;

            //TODO KG:DevMainForm a taşınmalı.. Orada çalışmadığı için şimdilik buraya taşındı.
            string StartupRibbonPage = Session.PrgSettingsOrg.StartupRibbonPage.ToStr();
            if (StartupRibbonPage != "")
                ribbon.SelectedPage = ribbon.Pages[Session.PrgSettingsOrg.StartupRibbonPage];

            this.Show();
        }

        void jobOP_PopupClicked(JobManager.Job popupAlert)
        {
            ShowMdiChildForm(typeof(ConfExpert.Forms.Depo.OverProduction.OPOnayiniziBekleyen));
        }

        void jobOP_Alert(JobManager.Job popupAlert)
        {
            if (Session.DataManager.ExecuteScalar("CheckOP", WhoAmI).ToString() != "")
                popupAlert.ShowPopup("Onayınızı bekleyen imalat fazlası girişlerini görmek için tıklayınız...");
        }

        XForm x = new XForm();
        void GridViewExtensions_Binding(DevExpress.XtraGrid.Views.Grid.GridView gridView, System.Windows.Forms.BindingSource bs)
        {
            x.InitGridView(gridView, bs);
        }

        void MainForm_UserChanged()
        {
            ConfExpertRibbon.Init();
        }

        void MainForm_Persisting()
        {
            Session.DataManager.ExecuteNonQuery("UpdGle", AppSession.CurrentUser.UserId, AppSession.gleCustomer, AppSession.gleSeason, AppSession.gleVendor);
        }


        void jobTasks_Alert(JobManager.Job popupAlert)
        {
            DataTable Tasks = Session.DataManager.ExecuteDataTable("TM_GetPendingTasks", WhoAmI);
            string message = "";

            if (Tasks.Rows.Count > 0)
                message = Tasks.Rows.Count + " Bekleyen Görev";

            SetTaskMessage(message, OnTaskClicked);

            if (Tasks.Select("Header='DO'").Length > 0)
                Application.DoEvents();
        }

        void jobYeniSiparis_PopupClicked(JobManager.Job popupAlert)
        {
            int id = AppSession.Lookups.Settings.FindByName("NewOrder").Val.ToInt();
            ShowMdiChildForm(typeof(Depo.Kauf.Orders), id);
        }

        void jobYeniSiparis_Alert(JobManager.Job popupAlert)
        {
            if (AppSession.Lookups.Settings.FindByName("NewOrder") != null)
                if (Session.CurrentUser.AmI(55, 3))
                    popupAlert.ShowPopup("Onayınızı bekleyen yeni sipariş var!\nSiparişleri görmek için tıklayınız...");
        }

        void jobOnay_Alert(JobManager.Job popupAlert)
        {
            if (AppSession.Lookups.Settings.FindByName("OnayBekleyen") != null)
                if (AppSession.Lookups.Settings.FindByName("OnayBekleyen").Val != string.Empty)
                    popupAlert.ShowPopup("Onayınızı bekleyen " + AppSession.Lookups.Settings.FindByName("OnayBekleyen").Val + " istek var.\nLütfen, Depo->Ayrımİstekleri altındaki OnayımıBekleyenİstekler formundan tercih bildirin.");

        }

        void jobPublish_Alert(JobManager.Job popupAlert)
        {
            if (Session.Version != null)
                if (AppSession.Lookups.Settings.FindByName("PublishVersion").Val.CompareTo(Session.Version) > 0)
                    popupAlert.ShowPopup("ConfExpert v." + AppSession.Lookups.Settings.FindByName("PublishVersion").Val + " hazır. \nLütfen çalışmalarınızı kaydederek programı yeniden başlatın.");
        }


        public void OnTaskClicked()
        {
            ShowMdiChildForm(typeof(Tasks));
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (Environment.MachineName == "KGULCAN")
            {
                if (e.KeyCode == Keys.F1)
                    AppSession.MainForm.ShowMdiChildForm(typeof(ConfExpert.Forms.Test.KG));

            }
        }
    }
}